package pers.zb.center.service.order.api.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;
import pers.zb.center.common.core.util.JsonUtil;
import pers.zb.center.service.order.api.OrderQueueMessageService;
import pers.zb.center.service.order.entity.OrderInfo;
import pers.zb.center.service.order.vo.OrderSync;

import javax.annotation.Resource;
import javax.jms.Queue;

@Service("orderQueueMessageServiceImpl")
public class OrderQueueMessageServiceImpl implements OrderQueueMessageService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 订单入库，消息发送模版
     */
    @Resource(name = "orderSyncTemplate")
    private JmsTemplate orderSyncTemplate;

    @Resource(name = "orderSyncSuccessWechatTemplateMessageTemplate")
    private JmsTemplate orderSyncSuccessWechatTemplateMessageTemplate;

    //订单同步消息目的地
    //@Resource(name = "orderSyncQueueDestination")
    //private Queue orderSyncQueueDestination;


    /**
     * 订单同步，发送微信模板消息通知， 消息目的地
     */
    @Resource(name = "orderSyncWechatTemplateMessageQueueDestination")
    private Queue orderSyncWechatTemplateMessageQueueDestination;

    @Override
    public void sendOrderSyncQueueMessage(OrderSync orderSync) {
        logger.debug("OrderSyncQueueMessageProducer【订单入库】， 消息生产者开始发送消息：", JsonUtil.toJson(orderSync));

        orderSyncTemplate.convertAndSend(orderSync);//如果配置文件中指定了目的地，可以使用这句话发送消息。

        //jmsTemplate.convertAndSend(this.orderSyncQueueDestination, order);
    }

    @Override
    public void sendOrderSyncTemplateMessage(OrderSync orderSync) {
        logger.debug("OrderSyncQueueMessageProducer【订单入库成功，发送微信模板消息】， 消息生产者开始发送消息：", JsonUtil.toJson(orderSync));

        orderSyncSuccessWechatTemplateMessageTemplate.convertAndSend(orderSync);//如果配置文件中指定了目的地，可以使用这句话发送消息。

        //jmsTemplate.convertAndSend(this.orderSyncWechatTemplateMessageQueueDestination, orderSync);

        // 发送之后，这里变成消息消费者，等待那边给我发送确认消息
        /*
         * Connection connection =
         * getJmsTemplate().getConnectionFactory().createConnection(); Session
         * session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
         * Destination recall_destination = session.createQueue("recall_queue");
         * MessageConsumer replyConsumer
         * =session.createConsumer(recall_destination); // 这里我们用个消息监听
         * replyConsumer.setMessageListener(new MessageListener() {
         * 
         * @Override public void onMessage(Message message) { TextMessage
         * textMessage = (TextMessage) message; try {
         * System.out.println(textMessage.getText()); } catch (JMSException e) {
         * e.printStackTrace(); } } });
         */
    }

}
